<template>
	<z-paging bg-color="#F4F6F8" ref="paging" v-model="list" @query="getList" empty-view-text="暂无数据"
		:empty-view-img-style="{width:'150rpx',height:'150rpx'}" :empty-view-title-style="{marginTop: '30rpx'}">
		<template #refresher="{refresherStatus}">
			<!-- 此处的custom-refresh为demo中自定义的组件，非z-paging的内置组件，请在实际项目中自行创建。这里插入什么view，下拉刷新就显示什么view -->
			<custom-refresher :status="refresherStatus" />
		</template>
		<view class="">
			<view class="font-28" v-if="list.length > 0">
				<view v-for="item in list" :key="item.id" class="mar-bottom-30 bg-white pad-30">
					<view class="betweenX centerY">
						<view class="color-text flex-w">申请状态：
							<text v-if="item.verify_status == 1">待审核</text>
							<text v-else-if="item.verify_status == 3">审核通过</text>
							<text v-else-if="item.verify_status == 4">未通过</text>
							<text v-else-if="item.verify_status == 5">撤销</text>
						</view>
						<view v-if="item.verify_status == 3" @click="hanldeDownload(item)" class="button-style">下载发票
						</view>
					</view>
					<view class="mar-top-10 color-text">时间:
						{{timeFormat(item.created_at, 'yyyy-mm-dd hh:MM:ss')}}
					</view>

					<view class="mar-top-30">发票详情：</view>
					<view class="mar-top-24 color-646464">
						发票类型：{{item.invoice_title_info.invoice_type == 1 ? '个人' : '公司'}} </view>
					<view class="mar-top-24 color-646464">
						{{item.invoice_title_info.invoice_type == 1 ? '个人发票抬头': '公司名称'}}：{{item.invoice_title_info.name}}
					</view>
					<template v-if="item.invoice_title_info.invoice_type == 2">
						<view class="mar-top-24 color-646464">税号：{{item.invoice_title_info.tax_no}}</view>
						<view class="color-646464">公司电话：{{item.invoice_title_info.company_phone}}</view>
						<view class="mar-top-24 color-646464">开户行：{{item.invoice_title_info.bank_name}}</view>
						<view class="mar-top-24 color-646464">开户行卡号：{{item.invoice_title_info.bank_card_no}}</view>
						<view class="mar-top-24 color-646464">公司注册地址：{{item.invoice_title_info.company_address}}</view>
					</template>
				</view>
			</view>
		</view>
	</z-paging>
</template>

<script setup>
	import { ref } from 'vue';
	import { onLoad } from '@dcloudio/uni-app';
	import { invoiceApplyLog } from '@/api/user.js';
	import { timeFormat } from '@/node_modules/uview-plus';

	const list = ref([]);
	const param = ref({});
	const paging = ref(null);
	const checkboxValue = ref([]);

	// 确认提交
	const submit = () => {
		if (checkboxValue.value.length > 0) {
			uni.setStorageSync('saveId', checkboxValue.value);
			uni.navigateBack();
		} else {
			uni.$u.toast('请选择要开的票');
		}
	}

	// 下载保存到手机相册
	const hanldeDownload = (row) => {



		// #ifdef MP-WEIXIN
		// 下载并保存图片到手机相册
		uni.downloadFile({
			url: row.download_url, // 图片的URL
			success: (downloadResult) => {
				if (downloadResult.statusCode === 200) {
					const regex = /\.(png|jpe?g|jpng|gif)$/i; // i 表示忽略大小写
					if (regex.test(row.download_url)) {
						// 下载成功后获取文件路径
						const filePath = downloadResult.tempFilePath;

						// 保存到相册
						uni.saveImageToPhotosAlbum({
							filePath: filePath,
							success: () => {
								uni.showToast({
									title: '图片已保存到相册',
									icon: 'success'
								});
							},
							fail: (err) => {
								uni.showToast({
									title: '保存失败',
									icon: 'none'
								});
							}
						});
					} else {
						uni.saveFile({
							tempFilePath: downloadResult.tempFilePath, // 临时文件路径
							success: function(saveRes) {
								const savedFilePath = saveRes.savedFilePath;
								uni.showToast({
									title: '文件已保存到：' + savedFilePath,
									icon: 'none'
								});

								setTimeout(() => {
									if (savedFilePath.endsWith('.pdf')) {
										wx.openDocument({
											filePath: savedFilePath, // 本地文件路径
											success: function(res) {
												console.log('文件打开成功');
											},
											fail: function(err) {
												console.error('文件打开失败:',
													err);
											}
										});
									}
								}, 2000)
							},
							fail: function(err) {
								uni.showToast({
									title: '保存失败',
									icon: 'none'
								});
							}
						});
					}
				} else {
					uni.showToast({
						title: '下载失败',
						icon: 'none'
					});
					console.error('下载失败:', downloadResult);
				}
			},
			fail: (err) => {
				uni.showToast({
					title: '下载失败',
					icon: 'none'
				});
				console.error('下载失败:', err);
			}
		});
		// #endif
	}

	// 获取列表数据
	const getList = (page, pageSize) => {
		invoiceApplyLog({
			page,
			ps: pageSize,
		}).then(res => {
			if (res?.code == 200) {
				paging.value.complete(res.data.data);
			} else {
				paging.value.complete(false);
			}
		})
	}

	onLoad((option) => {
		param.value = option;
	})
</script>

<style scoped>
	.button-style {
		width: 200rpx;
		padding: 10rpx 0;
		text-align: center;
	}

	.list {
		width: 100%;
	}

	/deep/.u-checkbox__label-wrap {
		width: 100%;
	}
</style>